package com.zhou.common.mybatis.template.shiro;

import org.flowable.engine.*;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.task.api.Task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DefaultProcessEngineInit {

    private static RuntimeService runtimeService;

    private static IdentityService identityService;


    public static void main(String[] args) {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();


        TaskService taskService = processEngine.getTaskService();
        FormService formService = processEngine.getFormService();
        // 指定flowable.cfg.xml 和beanName的几种方式
        /*ProcessEngineConfiguration.
                createProcessEngineConfigurationFromResourceDefault();
        createProcessEngineConfigurationFromResource(String resource);
        createProcessEngineConfigurationFromResource(String resource, String beanName);
        createProcessEngineConfigurationFromInputStream(InputStream inputStream);
        createProcessEngineConfigurationFromInputStream(InputStream inputStream, String beanName);*/
        identityService = processEngine.getIdentityService();

        RepositoryService repositoryService = processEngine.getRepositoryService();
        List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().active().list();
        list.forEach(System.out::println);

        ProcessDefinition test1 = repositoryService.createProcessDefinitionQuery().processDefinitionKey("test1").singleResult();

        runtimeService = processEngine.getRuntimeService();
        startProcessAsUser("test1", "starter1");
//        runtimeService.startProcessInstanceById("test1");


        List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("test_group").list();
        tasks.forEach(System.out::println);
        tasks = taskService.createTaskQuery().taskCandidateUser("test").list();
        tasks.forEach(System.out::println);
        tasks = taskService.createTaskQuery().taskAssignee("xianfeng").list();
        tasks.forEach(System.out::println);

        Map<String, Object> variables = new HashMap<String, Object>();
        //设置审批人
        variables.put("test1", "xianfeng");
        /*ProcessInstance processInstance =
                runtimeService.startProcessInstanceByKey("test1", variables);

        System.out.println(processInstance);*/

        tasks = taskService.createTaskQuery().taskCandidateGroup("test_group").list();
        tasks.forEach(System.out::println);
        tasks = taskService.createTaskQuery().taskCandidateUser("test").list();
        tasks.forEach(System.out::println);
        tasks = taskService.createTaskQuery().taskAssignee("xianfeng").list();
        tasks.forEach(System.out::println);
        taskService.complete(tasks.get(0).getId());

        /*String taskFormKey = formService.getTaskFormKey(tasks.get(0).getProcessDefinitionId(), tasks.get(0).getTaskDefinitionKey());
        TaskFormData taskFormData = formService.getTaskFormData(tasks.get(0).getId());
        System.out.println(taskFormKey);
        System.out.println(taskFormData);
        FormInfo taskFormModel = taskService.getTaskFormModel(tasks.get(0).getId());
        System.out.println(taskFormModel);

        taskService.completeTaskWithForm("taskId","formDefinitionId","outcome", Maps.newHashMap());
*/

    }

    private static String startProcessAsUser(String processKey, String userId) {
        try {
            identityService.setAuthenticatedUserId(userId);
            return runtimeService.startProcessInstanceByKey(processKey).getId();
        } finally {
            identityService.setAuthenticatedUserId(null);
        }
    }
}
